var mapSourceBox = null;
var addList = new Array();
var layers = new Array();
var curMode = null;
var map = null;
var geocoder = null;
var viewMode = "visible";
var mapOptions = null;
var wwLayer = null;
var osmLayer = null;

function initialize() {
  resetMap(mode);
}

function resetMap(mode) {
  if(curMode == "osm" || curMode =="ww") {
      map.destroy();
  } else if (curMode == "gmaps") {
  	GUnload();
  } else {
  }

  if (mode == "gmaps") {
      if(!document.getElementById("showMapInfoLink")) {
      } else {
      	document.getElementById("showMapInfoLink").style.display = "none";
      	if(!document.getElementById("mapInfo")) {
      	} else {
	      	document.getElementById("mapInfo").style.display = "none";
	}
      }      	
      map = new GMap2(document.getElementById("map"));
      map.setCenter(new GLatLng(lat, long), zoom); 
      var mapControl = new GMapTypeControl();
      map.addControl(mapControl);
      map.removeMapType(G_SATELLITE_MAP);
      geocoder = new GClientGeocoder();
      showAddress(addList[0],"see");
      showMultiAddress(addList);
      curMode = "gmaps";
    } else if (mode == "osm") {   
      if(!document.getElementById("showMapInfoLink")) {
      } else {
    	document.getElementById("showMapInfoLink").style.display = "inline";
      	document.getElementById("showMapInfoLink").innerHTML = "[Show Map Info]";
      	if(!document.getElementById("mapInfo")) {
      	} else {
	      	document.getElementById("mapInfo").style.display = "none";
	}
      }             
      mapOptions = {'maxResolution': 360/512/16, 'numZoomLevels':15, controls: [ new OpenLayers.Control.Navigation(), new OpenLayers.Control.PanZoomBar() ]};
      map = new OpenLayers.Map('map', mapOptions);
      osmLayer = new OpenLayers.Layer.WMS( "OSM", 
               [
                 "http://t1.hypercube.telascience.org/tiles?",
                 "http://t2.hypercube.telascience.org/tiles?",
                 "http://t3.hypercube.telascience.org/tiles?",
                 "http://t4.hypercube.telascience.org/tiles?"
                 ], 
                {layers: 'osm-4326', format: 'image/png'});
      map.addLayers([osmLayer]);
      map.addControl(new OpenLayers.Control.LayerSwitcher());
      map.setCenter(new OpenLayers.LonLat(long, lat), 10);     
      curMode = "osm";
    } else if (mode == "ww") {
      if(!document.getElementById("showMapInfoLink")) {
      } else {
      	document.getElementById("showMapInfoLink").style.display = "inline";
      	document.getElementById("showMapInfoLink").innerHTML = "[Show Map Info]";   
      	if(!document.getElementById("mapInfo")) {
      	} else {
	      	document.getElementById("mapInfo").style.display = "none";
	}      	
      }         
      mapOptions = { maxResolution: 1.6, numZoomLevels: 21 };
      map = new OpenLayers.Map('map', mapOptions);
      wwLayer = new OpenLayers.Layer.WorldWind( "Urban",
          "http://worldwind25.arc.nasa.gov/tile/tile.aspx?", .8, 9,
          {T:"104"}, { tileSize: new OpenLayers.Size(512,512) });
      map.addLayers([wwLayer]);
      map.addControl(new OpenLayers.Control.LayerSwitcher());
      map.setCenter(new OpenLayers.LonLat(long, lat), 15);  
      curMode = "ww";      
    } else {
      alert("Map not available");
    }
}

function showMapInfo() {    
  if(document.getElementById("showMapInfoLink").innerHTML == "[Hide Map Info]") {
      document.getElementById("mapInfo").innerHTML = "";
      document.getElementById("showMapInfoLink").innerHTML = "[Show Map Info]";  
  } else {
      layers = map.getLayersBy("visibility", true);
      mapSourceBox = document.getElementById("sourceFrmBox");
      mapSourceBox.innerHTML += "<div id='mapInfo'><h3>Map Information</h3><ul><li>Zoom Levels: " + layers[0].zoomLevels + "</li><li>LZD: " + layers[0].lzd + "</li><li>Is Base Layer: " + layers[0].isBaseLayer + "</li></ul><form id='mapInfoFrm' action='self' method='post'><input id='mapInfoFrmSubmit' type='button' value='Submit' onclick='return updateMapInfo();' /></form></div>";	
      document.getElementById("showMapInfoLink").innerHTML = "[Hide Map Info]";  
  }  
}    

function updateMapInfo() {

}

function showMultiAddress(addList) {
  for ( i=0; i < addList.length; i++ )
  {
    showAddress ( addList[i] , "see");
  }
}

function showAddress(address, poi) {

  // Create our "tiny" marker icon
  var mapIcon = new GIcon(G_DEFAULT_ICON);
  if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
             // alert(address + " not found");
            } else {
             // map.setCenter(point, 15);
      	      if (poi == "see") {
      	      	poiIcon = MapIconMaker.createMarkerIcon({width: 24, height: 24, primaryColor: "#00ff00"});
      	      } else if (poi == "eat") {
      	      	poiIcon = MapIconMaker.createMarkerIcon({width: 24, height: 24, primaryColor: "#00fffb"});
      	      } else if (poi == "do") {
      	      	poiIcon = MapIconMaker.createMarkerIcon({width: 24, height: 24, primaryColor: "#7000ff"});
      	      } else {
      	        poiIcon = MapIconMaker.createMarkerIcon({width: 24, height: 24, primaryColor: "#0052ff"});
      	      }
			
	      var marker = new GMarker(point, {icon: poiIcon});
              map.addOverlay(marker);
            }
          }
        );
      }
}

function hideMap(){
  if(document.getElementById("ca-hidemap").childNodes[0].innerHTML == "Hide map") {
	  document.getElementById("mapbox").style.display="none";
	  document.getElementById("ca-hidemap").getElementsByTagName('a')[0].innerHTML = "Show map";  
  } else {
  	  document.getElementById("mapbox").style.display="inline";
	  document.getElementById("ca-hidemap").getElementsByTagName('a')[0].innerHTML = "Hide map";  
  }
}

function showAddressGeocoder(){
  if(document.getElementById("ca-geocode").childNodes[0].innerHTML == "Hide geocode") {
	  document.getElementById("addressGeocode").style.display="none";
	  document.getElementById("ca-geocode").getElementsByTagName('a')[0].innerHTML = "Geocode";  
  } else {
          document.getElementById("addressGeocode").style.display="inline";
	  document.getElementById("ca-geocode").getElementsByTagName('a')[0].innerHTML = "Hide geocode";  
  }
}


function catchEvent(eventObj, event, eventHandler) {
   if (eventObj.addEventListener) {
       eventObj.addEventListener(event, eventHandler,false);
   } else if (eventObj.attachEvent) {
       event = "on" + event;
       eventObj.attachEvent(event, eventHandler);
   }
}

function cancelEvent(event) {
   if (event.preventDefault) {
       event.preventDefault();
       event.stopPropagation();
   } else {
       event.returnValue = false;
       event.cancelBubble = true;
   }
}

if (window.addEventListener) window.addEventListener("load",initialize,false);
else if (window.attachEvent) window.attachEvent("onload",initialize);